package demo.practice.backtrack;

import java.util.ArrayList;
import java.util.List;

public class P78 {

    public static void main(String[] args) {
        P78 p78= new P78();
        p78.subsets(new int[]{1,2,3});
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        subsets(0, nums, list);
        return list;
    }

    private void subsets(int index, int[] nums, List<List<Integer>> list) {
        if (index == nums.length) {
            list.add(new ArrayList<>());
            return;
        }
        //[],[1],[2][1,2]
        //[],[1],[2],[1,2],   [3],[1,3],[2,3],[1,2,3]
        int cur = nums[index];
        subsets(index+1, nums, list);

        List<List<Integer>> thislevel= new ArrayList<>();
        list.forEach(l -> {

            List<Integer> tl = new ArrayList<>();
            l.forEach(ll -> tl.add(ll));
            tl.add(cur);
            thislevel.add(tl);
        });

        list.addAll(thislevel);
    }
}
